//
// Created by madison on 2022/7/18.
//

#include "queue"

using namespace std;

/**
 * Definition for a binary tree node.
 **/
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    // 方法一：深度优先搜索
    int maxDepth(TreeNode *root) {
        if (root == nullptr) return 0;
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }

    // 方法二：广度优先搜索
    int maxDepth1(TreeNode *root) {
        if (root == nullptr) return 0;
        queue<TreeNode *> Q;
        Q.push(root);
        int ans = 0;
        while (!Q.empty()) {
            int sz = Q.size();
            while (sz > 0) {
                TreeNode *node = Q.front();
                Q.pop();
                if (node->left) {
                    Q.push(node->left);
                }
                if (node->right) {
                    Q.push(node->right);
                }
                sz -= 1;
            }
            ans += 1;
        }
        return ans;
    }
};

int main() {
    TreeNode *node3 = new TreeNode(15);
    TreeNode *node4 = new TreeNode(7);

    TreeNode *node1 = new TreeNode(9);
    TreeNode *node2 = new TreeNode(20, node3, node4);

    TreeNode *root = new TreeNode(3, node1, node2);

    Solution solution;
    printf("%d", solution.maxDepth1(root));

    return 0;
}